package jmind.core.serialize;

import java.io.*;

/**
 * description:
 *
 * @author weibo.xie
 * @date : create in 5:17 下午 2021/5/13
 */
public class JdkSerializer implements Serializer {
    @Override
    public byte[] serialize(Object object) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            //   GZIPOutputStream outputStream = new GZIPOutputStream(baos);
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } finally {
            try {
                oos.close();
                baos.close();
            } catch (Exception e) {
            }
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = null;
        try {
            //   GZIPInputStream inputStream = new GZIPInputStream(bais);
            ois = new ObjectInputStream(bais);
            Object object = ois.readObject();
            return object;
        } finally {
            try {
                ois.close();
                bais.close();
            } catch (Exception e) {
            }
        }
    }
}